package common;

import java.util.ArrayList;

public class Strings
{
	// Парсим строки вида КЛЮЧ = ЗНА_ЧЕНИЕ и КЛЮЧ = "ТЕКСТ"
	public static ArrayList<String> getSimpleKeyEquSting(String inpString)
	{
		return getStrings(inpString, false, "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz0123456789_.", " =");
	}

	public static ArrayList<String> getTextAndComment(String inpText)
	{
		String text = null, comment = null;
		inpText = inpText.trim();
		if (inpText.length() != 0)
		{
			int quot = -1;
			if (inpText.charAt(0) == '\'' || inpText.charAt(0) == '"')
				quot = 0;
			int i;
			for (i = 1; i < inpText.length(); i++)
			{
				if (quot < 0)
				{
					if (inpText.charAt(i) == '\'' || inpText.charAt(i) == '"')
						quot = i;
					else if (inpText.charAt(i) == '/' && inpText.charAt(i - 1) == '/')
					{
						text = inpText.substring(0, i - 1).trim();
						comment = inpText.substring(i + 1);
						break;
					}
				}
				else
				{
					if (inpText.charAt(i) == inpText.charAt(quot) && inpText.charAt(i) != '\\')
					{
						quot = -1;
					}
				}
			}
			if (i >= inpText.length())
				text = inpText;
		}

		ArrayList<String> out = new ArrayList<String>();
		out.add((text == null) ? "" : Strings.trinAndDeleteAllDoubleSpace(text));
		out.add((comment == null) ? "" : Strings.trinAndDeleteAllDoubleSpace(comment));

		return out;
	}

	public static ArrayList<String> getStrings(String inpString, boolean parseText, String termChars, String separateChars)
	{
		ArrayList<String> outStrings = new ArrayList<String>();
		inpString = inpString.trim();
		if (inpString.length() == 0)
			return outStrings;

		boolean isTerm = (termChars.indexOf(inpString.charAt(0)) >= 0);
		boolean isQuoteText = false;

		String temp = "";
		for (int i = 0; i < inpString.length(); i++)
		{
			if (isTerm)
			{
				if (isQuoteText && parseText || termChars.indexOf(inpString.charAt(i)) >= 0)
				{
					temp += inpString.charAt(i);
					if (("'\"").indexOf(inpString.charAt(i)) >= 0)
						isQuoteText = !isQuoteText;
				}
				else if (separateChars.indexOf(inpString.charAt(i)) >= 0)
				{
					outStrings.add(temp);
					temp = "";
					isTerm = false;
					i--;
				}
				else
					return null;
			}
			else
			{
				if (separateChars.indexOf(inpString.charAt(i)) >= 0)
					temp += inpString.charAt(i);
				else if (termChars.indexOf(inpString.charAt(i)) >= 0)
				{
					isTerm = true;
					temp = "";
					i--;
				}
				else
					return null;
			}
		}
		if (isTerm)
			outStrings.add(temp);

		return outStrings;
	}
	
	public static int getShift(String text)
	{
		int i=0;
		while (i<text.length() && text.charAt(i)==' ')
			i++;
		return i;
	}

	public static String trinAndDeleteAllDoubleSpace(String str)
	{
		str=str.trim();
		while (str.contains("  "))
			str=str.replace("  "," ");
		return str;
	}

	/*public static ArrayList<String> getGroups(String inpString, boolean quoteParse, boolean groupsContainerParse, String... groups)
	{
		inpString=trinAndDeleteAllDoubleSpace(inpString);
		ArrayList<String> outStrings=new ArrayList<String>();
		if(inpString.length()==0)
			return outStrings;

		int groupStart=0;
		int groupNum;
		String tStr="";
		for(int iGroups=0;iGroups<groups.length;iGroups++)
			if(groups[iGroups].indexOf(inpString.charAt(0))>=0)
			{
				groupNum=iGroups;
				break;
			}

		for(int i=0;i<inpString.length();i++)
		{
			if(groupStart>=0)
			{
				if()
			} // if(groupStart>=0)
		}

		return outStrings;
	}*/
}
